package services

import (
	"context"
	"encoding/json"
	"fmt"

	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay/models"
)

// GetCardScenario 获取卡场景列表
func GetCardScenario(ctx context.Context, client *juswaypay.JuswayPayClient) (*models.CardScenarioResponse, error) {
	path := "/v1/card/scenario"

	resp, err := client.Get(ctx, path)
	if err != nil {
		return nil, fmt.Errorf("get card scenario failed: %v", err)
	}

	var result models.CardScenarioResponse
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %v", err)
	}

	if !result.Ret {
		return nil, fmt.Errorf("get card scenario failed: status=%d", result.Status)
	}

	return &result, nil
}

// GetCardBinRange 获取卡头列表
func GetCardBinRange(ctx context.Context, client *juswaypay.JuswayPayClient) (*models.CardBinRangeResponse, error) {
	path := "/v1/card/bin-range"

	resp, err := client.Get(ctx, path)
	if err != nil {
		return nil, fmt.Errorf("get card bin range failed: %v", err)
	}

	var result models.CardBinRangeResponse
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %v", err)
	}

	if !result.Ret {
		return nil, fmt.Errorf("get card bin range failed: status=%d", result.Status)
	}

	return &result, nil
}

// GetChannelList 获取通道列表
func GetChannelList(ctx context.Context, client *juswaypay.JuswayPayClient) (*models.ChannelListResponse, error) {
	path := "/v1/channel/list"

	resp, err := client.Get(ctx, path)
	if err != nil {
		return nil, fmt.Errorf("get channel list failed: %v", err)
	}

	var result models.ChannelListResponse
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %v", err)
	}

	if !result.Ret {
		return nil, fmt.Errorf("get channel list failed: status=%d", result.Status)
	}

	return &result, nil
}

// CreateCardHolder 创建持卡人
func CreateCardHolder(ctx context.Context, client *juswaypay.JuswayPayClient, req *models.CardHolderRequest) (*models.CardHolderResponse, error) {
	path := "/v1/card/holder"

	resp, err := client.Post(ctx, path, req)
	if err != nil {
		return nil, fmt.Errorf("create card holder failed: %v", err)
	}

	var result models.CardHolderResponse
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %v", err)
	}

	if !result.Ret {
		return nil, fmt.Errorf("create card holder failed: status=%d", result.Status)
	}

	return &result, nil
}

// ApplyCard 申请开卡
func ApplyCard(ctx context.Context, client *juswaypay.JuswayPayClient, req *models.CardApplyRequest) (*models.CardApplyResponse, error) {
	path := "/v1/card/apply"

	resp, err := client.Post(ctx, path, req)
	if err != nil {
		return nil, fmt.Errorf("apply card failed: %v", err)
	}

	var result models.CardApplyResponse
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %v", err)
	}

	if !result.Ret {
		return nil, fmt.Errorf("apply card failed: status=%d", result.Status)
	}

	return &result, nil
}

// RechargeCard 充值
func RechargeCard(ctx context.Context, client *juswaypay.JuswayPayClient, req *models.CardRechargeRequest) (*models.CardRechargeResponse, error) {
	path := "/v1/card/recharge"

	resp, err := client.Post(ctx, path, req)
	if err != nil {
		return nil, fmt.Errorf("recharge card failed: %v", err)
	}

	var result models.CardRechargeResponse
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %v", err)
	}

	if !result.Ret {
		return nil, fmt.Errorf("recharge card failed: status=%d", result.Status)
	}

	return &result, nil
}

// RefundCard 卡片退款
func RefundCard(ctx context.Context, client *juswaypay.JuswayPayClient, req *models.CardRefundRequest) (*models.Response, error) {
	// 构建请求参数
	params := map[string]interface{}{
		"cardId":       req.CardID,
		"refundAmount": req.RefundAmount,
		"twoPassword":  req.TwoPassword,
	}
	if req.Remark != "" {
		params["remark"] = req.Remark
	}

	// 发送请求
	resp, err := client.Post(ctx, "/api/v1/card/refund", params)
	if err != nil {
		return nil, fmt.Errorf("refund card failed: %v", err)
	}

	// 解析响应
	var response models.Response
	if err := json.Unmarshal(resp, &response); err != nil {
		return nil, fmt.Errorf("parse response failed: %v", err)
	}

	return &response, nil
}
